<?php
namespace app\index\controller;

use app\common\model\ZhIntroduce as ZhIntroduceModel;
use app\common\model\Company as CompanyModel;

/**
 * 寺院介绍控制器
 * Class Zhintroduce
 * @package app\index\controller
 */
class Zhintroduce extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhIntroduceModel;
        $cmodel = new CompanyModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try {
            if(!empty($param['title'])){
                $map['title'] = $param['title'];
            }
            $map['comid'] = $user['comid'];
            $map['status'] = ['neq',3];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'id,title,content,status,create_time');
            $company = $cmodel->findData($user['comid'],'id','id,content,addtime');
            $arr[0]['id'] = $company['id'];
            $arr[0]['title'] = '寺院简介';
            $arr[0]['content'] = $company['content'];
            $arr[0]['type'] = '默认';
            $arr[0]['status'] = 1;
            $arr[0]['create_time'] = date('Y-m-d H:i:s',$company['addtime']);
            $lists = array_merge($lists,$arr);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhIntroduceModel;
        $user = $this->user;
        try{
            $param['comid'] = $user['comid'];
            $model->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ZhIntroduceModel;
        try{
            $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
}